using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace TestMvc {
    public class TestMiddleware {
        private readonly RequestDelegate _next;
        private ILogger _logger;

        public TestMiddleware (RequestDelegate next, ILoggerFactory loggerFactory) {
            _next = next;
            _logger = loggerFactory.CreateLogger<TestMiddleware> ();
        }

        public async Task Invoke (HttpContext context) {

            _logger.LogInformation ("TestMiddleWare Invoke Begin");
            // await context.Response.WriteAsync("form middle ware");
            await _next.Invoke (context);
            _logger.LogInformation ("TestMiddleWare Invoke END");
        }
    }

    public static class TestMiddlewareExtension {
        public static IApplicationBuilder UseTestMiddelware (this IApplicationBuilder applicationBuilder) {
            return applicationBuilder.UseMiddleware<TestMiddleware> ();
        }
    }
}